package org.bingmaps.app;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import Data.Tracks;
import android.os.Bundle;
import android.app.Activity;

import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;


public class UserTracks extends Activity {

	private ListView listViewTracks;
	private Tracks MyTracks[];
	private String MyTracksString[];
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState);  
	       setContentView(R.layout.activity_user_tracks);
	       
	    
	       //TEMPORARY  Just to fill the listView
	       
	       File root = android.os.Environment.getExternalStorageDirectory();
	       String s = new String(root.getAbsolutePath()+ "/TrackingData");

	       listViewTracks = (ListView) findViewById(R.id.listViewTracks);
	       ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                   android.R.layout.simple_list_item_1, GetFiles(s));
	       
	       listViewTracks.setAdapter(adapter);
	       
	       
	       listViewTracks.setOnItemClickListener(new OnItemClickListener() {
	           public void onItemClick(AdapterView<?> parent, View view,
	               int position, long id) {
	                
	               // selected item 
	               
	        	   String Track = ((TextView) view).getText().toString();
	        	   
	        	   
	               // Launching new Activity on selecting single List Item
	        	   Intent i = new Intent(getApplicationContext(), MapShowerActivity.class);
	              
	        	   i.putExtra("product", Track);
	        	   
	        	   // sending data to new activity
	              startActivity(i);
	              
	           }
	         });
	     }
	       
	
	public ArrayList<String> GetFiles(String DirectoryPath) {
	    ArrayList<String> MyFiles = new ArrayList<String>();
	    File f = new File(DirectoryPath);

	    f.mkdirs();
	    File[] files = f.listFiles();
	    if (files.length == 0)
	        return null;
	    else {
	        for (int i=0; i<files.length; i++) 
	            MyFiles.add(files[i].getName());
	    }

	    return MyFiles;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.user_tracks, menu);
		return true;
	}

}
